package permissions

import (
	"github.com/beego/beego/v2/server/web/pagination"
	"xpms/controllers"
	"xpms/cores"
)

// 权限管理
type ManagePermissionController struct {
	controllers.BaseController
}

// @Title 权限
// @Description 分页查询权限数据
// @Param	p		query 	int	true		"第几页"
// @Param	pageoffset		query 	int			"每页数"
// @Param	keywords		query 	string			"关键字搜索"
// @Success 200 {object} groups.Permissions  int   int  int
// @Failure 403  not exist
// @router /page [get]
func (u *ManagePermissionController) PermissionByPage() {
	page, err := u.GetInt("p")
	if err != nil {
		page = 1
	}
	offset, err := u.GetInt("pageoffset")
	keywords := u.GetString("keywords")
	condArr := make(map[string]string)
	condArr["keywords"] = keywords
	countGroup := CountPermission(condArr)
	paginator := pagination.SetPaginator(u.Ctx, offset, countGroup)
	_, _, permissions := ListPermission(condArr, page, offset)
	type data struct {
		Permissions []Permissions
		Total       int64
		PageNums    int
		Page        int
	}
	d := &data{permissions, paginator.Nums(), paginator.PageNums(), paginator.Page()}
	var responseEntity cores.ResponseEntity
	u.Data["json"] = responseEntity.Build(d).Data
	u.Ctx.Output.Status = responseEntity.StatusCode
	u.ServeJSON()
}

// @Title 权限
// @Description 获取所有权限
// @Success 200 {object} groups.Permissions
// @Failure 403  not exist
// @router /list [get]
func (u *ManagePermissionController) PermissionByList() {
	condArr := make(map[string]string)
	_, _, permissions := ListPermission(condArr, 1, 3000)
	u.Data["json"] = permissions
	u.ServeJSON()
}
